
import {
    createStackNavigator,
    createMaterialTopTabNavigator,
    createSwitchNavigator,
    createDrawerNavigator
} from 'react-navigation'
import { Toast } from 'antd-mobile';
import { AsyncStorage } from 'react-native'
import LoginSceen from '../routers/home/Login';
import MainSceen from '../routers/home/Main';
import AuthLoadingScreen from '../routers/AuthLoading';
import UserSceen from '../routers/home/User';
import UserFunctionDrawer from '../routers/UserFunction';

const titleStyle = {
    headerStyle: {
        backgroundColor: '#fff'
    },
    //导航栏的title的style
    headerTitleStyle: {
        color: 'black',
        //居中显示
        alignSelf: 'left',
        fontSize: 15,
        fontWeight: 'normal'

    }
}

//主页导航
const RootNavigator = createMaterialTopTabNavigator(
    {
        Home: {
            screen: MainSceen,
            navigationOptions: {
                headerTitle: '首页',
                tabBarLabel: '首页',
                ...titleStyle
            },
        },
        User: {
            screen: UserSceen,
            navigationOptions: {
                headerTitle: '用户',
                tabBarLabel: '用户',
                ...titleStyle
            },
        }
    },
    {
        animationEnabled: true,
        swipeEnabled: true,
        tabBarPosition: 'bottom',
        lazy: true,
        initialRouteName: 'Home',
        tabBarOptions: {
            activeTintColor: 'rgba(236,97,73,.7)', // 文字和图片选中颜色
            inactiveTintColor: '#999', // 文字和图片未选中颜色
            showIcon: false, // android 默认不显示 icon, 需要设置为 true 才会显示
            indicatorStyle: {
                height: 0  // 如TabBar下面显示有一条线，可以设高度为0后隐藏
            },
            style: {
                backgroundColor: '#fff', // TabBar 背景色
            },
            labelStyle: {
                fontSize: 15, // 文字大小
            },
        },
        navigationOptions: {
            tabBarOnPress: async (obj) => {
                try {
                    const userData = await AsyncStorage.getItem('userInfo');
                    if (userData) {
                        obj.defaultHandler();
                    }
                    else {
                        obj.navigation.navigate('Login');
                    }
                } catch (e) {
                    Toast.info(e.message, 2, null, false);
                }
            }
        }

    }
);


//主导航
export const BaseNavigator = createDrawerNavigator({
    // 对应界面名称
    RootNavigator: {
        screen: RootNavigator,

    },
    UserFunctions: {
        screen: UserFunctionDrawer
    }
},
    {
        lazy: true,
        drawerWidth: 100, // 抽屉宽
        drawerPosition: 'left', // 抽屉在左边还是右边
    }
);

//登录页面
const LoginNavigation = createStackNavigator({ Login: LoginSceen });

export const MainNavigator = createSwitchNavigator(
    {
        Auth: AuthLoadingScreen,
        Main: BaseNavigator,
        Login: LoginNavigation
    }
    , {
        initialRouteName: 'Auth'
    }
);
